#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int b_index = 0;
int t_len = 0;
int count = 0;

void show_banner()
{
        int i;
        int len;
        int left;

        left = t_len - count;

        if (left == 0 || b_index < count)
                return;

        len = left > 32 ? 32 : ((left + 7) / 8) * 8;

        //printf("len = %d\n", len);


        if (b_index > 0)
                printf("-------------------------------------------------------"
                       "-------------------------------------------------------\n");
        for (i = left - 1; i >= left - len ; i--) {
                printf("%-2d ", i);
                if (i % 4 == 0)
                        printf("| ");
                count++;
        }
        printf("\n");
}

void show_bits(unsigned char ch)
{
        int i;

        show_banner();

        for (i = 7; i >= 0; i--) {
                printf("%-2d ", ch & (1 << i) ? 1 : 0);
                b_index++;

                if (i % 4 == 0)
                        printf("| ");

                if (b_index % 32 == 0)
                        printf("\n");
        }
}

unsigned char hex_to_d(char *ch)
{
        unsigned char ret = 0;
        unsigned char c;

        while (*ch) {

                if ( *ch >= '0' && *ch <= '9') {
                        c = *ch - '0';
                }
                else if (*ch >= 'a' && *ch <= 'f') {
                        c = *ch - 'a' + 10;
                }
                else if (*ch >= 'A' && *ch <= 'F') {
                        c = *ch - 'A' + 10;
                }
                
                ret = ret * 16 + c;
                ch++;
        }

        return ret;
}


void show_le(char *str)
{
        char *p;
        char ch[3];
        int flag;

        p = str;
        flag = strlen(str) % 2;
        
        while(*p) {

                memset(ch, 0, 3);
                if (flag) {
                        ch[0] = *p++;
                        flag = 0;
                        goto fast;
                }
                if (*(p + 1)) {
                        strncpy(ch, p, 2);
                        p += 2;
                }
                else {
                        ch[0] = *p;
                        p++;
                }
        fast:
                show_bits(hex_to_d(ch));
        }
        printf("\n");
}

void show_be(char *str)
{
        char *p;
        char ch[3];
        int flag;

        p = str + strlen(str) - 1;
        flag = strlen(str) % 2;
        while(p >= str) {
                memset(ch, 0, 3);
                if (flag) {
                        ch[0] = *p--;
                        flag = 0;
                        goto fast;
                }
                if (*(p - 1)) {
                        strncpy(ch, p - 1, 2);
                        p -= 2;
                }
                else {
                        ch[0] = *p;
                        p--;
                }
        fast:
                show_bits(hex_to_d(ch));
        }
        printf("\n");
}


int main(int argc, char *argv[])
{
        
        if (argc < 2) {
                printf("Usage: %s <hex str>\n", argv[0]);
                exit(1);
        }

        t_len = ((strlen(argv[1]) + 1) / 2) * 8;
#ifdef BE
	printf("Big Endian:\n");
	printf("\n");
	show_be(argv[1]);
#else
	printf("Little Endian:\n");
	printf("\n");
	show_le(argv[1]);
#endif
        return 0;
}
